// pages/monitoring/monitoring.js
var wxCharts = require('../../utils/wxcharts.js');
import request from '../../utils/config.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentNum: "0",
    executionList:new Array(),
    completeList: new Array()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options=====", options)
    let serviceName = "statisticsCurrInfos";
    if (options.type == 'enforce'){
      serviceName = "statisticsCurrInfos";
    } else if (options.type == 'lawsuit'){
      serviceName = "statisticsLawsuitCurrInfos";
    }
    wx.showLoading({
      title: '加载中...',
    })
    //获取数据
    let bodyInfo = {
      pageNum: '',
      pageSize: "",
    }
    let _that = this;
    request.requestGetMis_api(serviceName, wx.getStorageSync("token"),bodyInfo, (res) => {
      wx.hideLoading();
      console.log("获取案件执行情况成功=", res)
      let tempList = JSON.parse(res.data.body);
      let executionList = new Array();
      let completeList = new Array();
      if (tempList.length >0){
        tempList.forEach(function (e) {
          executionList.push({
            "group": e.deptName,
            "normal": e.unExpireAndIngCurrInfoVos.length,
            "timeout": e.expireAndIngCurrInfoVos.length,
            "breakoff": e.breakEnforceCurrInfoVos.length,
          })
          completeList.push({
            "group": e.deptName,
            "success": e.finishCurrInfoVos.length,
            "unsuccess": e.breakEnforceCurrInfoVos.length + e.expireAndIngCurrInfoVos.length + e.unExpireAndIngCurrInfoVos.length,
            "casebreak": e.backCasesEnforceCurrInfoVos.length,
          })
        });
        _that.setData({
          executionList: executionList,
          completeList: completeList
        });
        _that.createChart();
      }
    }, (res) => {
      console.log("获取案件执行情况失败=", res)
      wx.hideLoading()
    });
  },

  //点击进行中
  showUnderway: function(){
    this.setData({
      currentNum: "0"
    });
    if (this.data.executionList.length > 0 && this.data.completeList){
      this.createChart();
    }
  },

  //显示完成
  showFinish: function(){
    this.setData({
      currentNum: "1"
    });
    if (this.data.executionList.length > 0 && this.data.completeList) {
      this.createCompleteChart();
    }
    
  },

  //生成监控中图标
  createChart: function(){
    let groupArr = new Array(),arr1 = new Array(),arr2 = new Array(),arr3 = new Array();
    if (this.data.currentNum == '0'){
      let tempArray = this.data.executionList;
      tempArray.forEach(function(e){
        groupArr.push(e.group);
        arr1.push(e.normal);
        arr2.push(e.timeout);
        arr3.push(e.breakoff);
      });
    } 
    
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    let columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: groupArr,
      series: [{
        name: '正常',
        data: arr1,
        format: function (val, name) {
          return val.toFixed(0);
        },
        color: "#269326"
      }, {
        name: '超时',
        data: arr2,
        format: function (val, name) {
          return val;
        },
        color: "red"
      }, {
        name: '中断',
        data: arr3,
        format: function (val, name) {
          return val;
        },
        color: "#ffca2c"
      }],
      yAxis: {
        format: function (val) {
          return val + '件';
        },
        title: '案件数',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  },

  //生成案件完成图表
  createCompleteChart: function () {
    let groupArr = new Array(), arr1 = new Array(), arr2 = new Array(), arr3 = new Array();
    let tempArray = this.data.completeList;
    tempArray.forEach(function (e) {
      groupArr.push(e.group);
      arr1.push(e.success);
      arr2.push(e.unsuccess);
      arr3.push(e.casebreak);
    });

    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    let columnChart = new wxCharts({
      canvasId: 'columnCanvas2',
      type: 'column',
      animation: true,
      categories: groupArr,
      series: [{
        name: '已完成',
        data: arr1,
        format: function (val, name) {
          return val.toFixed(0);
        },
        color: "blue"
      }, {
        name: '未完成',
        data: arr2,
        format: function (val, name) {
          return val;
        },
        color: "red"
      }, {
        name: '已退案',
        data: arr3,
        format: function (val, name) {
          return val;
        },
        color: "#000000"
      }],
      yAxis: {
        format: function (val) {
          return val + '件';
        },
        title: '案件数',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})